package actions;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import services.base.DataSourceService;
import services.base.ExecuteSP;
import forms.LoginForm;

public class LoginAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		boolean loginFlag = false;
		LoginForm loginForm = (LoginForm) form;
		ExecuteSP esp = new ExecuteSP();
		ResultSet rs = null;
		String ret = "";
		// get database connection
		try {
			rs = esp.executeSP(" call gueseg.sp_get_usuarios('"
					+ loginForm.getUser() + "','" + loginForm.getPass() + "') ");
			if (rs.next()) {
				loginFlag = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			esp.closeConnection();
		}
		if (loginFlag) {
			return mapping.findForward("bien");
		} else {
			return mapping.findForward("mal");
		}

	}

}
